package com.test28_类库应用;

import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

interface IStringHandle {
    /** 数据拆分 */
    public String[] split();
}

/**
 * HTML内容拆分
 */
class HTMLStringHandle implements IStringHandle {
    private String content;
    private String regex;   // 拆分的正则表达式

    public HTMLStringHandle(String content, String regex) {
        this.content = content;
        this.regex = regex;
    }

    @Override
    public String[] split() {
        Matcher matcher = Pattern.compile(this.regex).matcher(this.content);
        StringBuffer buffer = new StringBuffer(this.content.length());
        while(matcher.find()) {
            String value = matcher.group(0);
            String[] temp = value.split("=");
            buffer.append(temp[0]).append(":").append(temp[1].replace("\"", "")).append("|");
        }
        return buffer.toString().split("\\|");
    }
}

class StringHandleFactory {
    private StringHandleFactory() {
    }

    public static IStringHandle getInstance(String content, String regex) {
        return new HTMLStringHandle(content, regex);
    }
}

/**
 * HTML结构拆分
 */
public class YootkDemo_05 {

    @Test
    public void test1() {
        String html = "<font face=\"Arial,Serif\" size=\"+2\" color=\"red\">";
        String regex = "\\w+=\"[a-zA-Z0-9,\\+]+\"";
        IStringHandle handle = StringHandleFactory.getInstance(html, regex);
        String[] result = handle.split();
        for (String temp : result) {
            System.out.println(temp);
        }
    }
}
